/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vita.vitasolution.service.api;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import vita.vitasolution.db.entities.User;
import vita.vitasolution.service.dto.LoginData;

/**
 *
 * @author harker777
 */
@Stateless
@Path("api/user")
public class UserApi {

	@PersistenceContext(unitName = "VitaSolutionPU")
	private EntityManager em;

	@POST
	@Path("login")
	@Consumes({"application/json"})
	@Produces({"application/json"})
	public String login(LoginData loginData) {
		CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
		CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();

		Root<User> root = criteriaQuery.from(User.class);
		criteriaQuery.select(root);
		criteriaQuery.where(criteriaBuilder.equal(root.get("login"), loginData.getLogin()));
		criteriaQuery.where(criteriaBuilder.equal(root.get("password"), loginData.getPassword()));
		Query query = em.createQuery(criteriaQuery);

		List<User> users = query.getResultList();
		if (users.isEmpty()) {
			return "\" Bad \"";
		} else {
			return "\" Good \"";
		}
	}
}
